package com.edu.controller;

import com.edu.beans.Book;
import com.edu.beans.Guest;
import com.edu.beans.Home;
import com.edu.beans.Vip;
import com.edu.service.BookService;
import com.edu.service.GuestService;
import com.edu.service.HomeService;
import com.edu.service.VipsService;
import com.edu.service.impl.BookServiceImpl;
import com.edu.service.impl.GuestServiceImpl;
import com.edu.service.impl.HomeServiceImpl;
import com.edu.service.impl.VipsServiceImpl;
import com.edu.utils.FormBeanUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/BooksServlet.do")
public class BooksServlet extends HttpServlet {

    HomeService homeService = new HomeServiceImpl();
    VipsService vipsService = new VipsServiceImpl();
    BookService bookService = new BookServiceImpl();
    GuestService guestService = new GuestServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        // 获取请求参数中的 action 字段
        String action = req.getParameter("action");
        switch (action) {
            case "loadData":
                loadData(resp);
                break;
            case "addBook":
                addBook(req, resp);
                break;
            case "bookList":
                bookList(req, resp);
                break;
            case "deleteBook":
                deleteBook(req, resp);
                break;
            case "goUpdate":
                goUpdate(req, resp);
                break;
            case "updateBook":
                updateBook(req, resp);
                break;
            default:
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Action not found");
        }
    }


    /**
     * 更新预定
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void updateBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Book book = FormBeanUtils.populate(req, Book.class);
        int result = bookService.updateBook(book);
        if (result > 0) {
            resp.getWriter().write("true");
        } else {
            resp.getWriter().write("false");
        }
    }

    /**
     * 跳转到更新预定页面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    private void goUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Book book = bookService.getBookById(id);
        Home home = homeService.getHomeById(book.getHome_id());
        Vip vip = vipsService.findVipByid(book.getVip_id());
        req.setAttribute("vip", vip);
        req.setAttribute("home", home);
        req.setAttribute("book", book);
        req.getRequestDispatcher("/views/guests_update.jsp").forward(req, resp);
    }

    /**
     * 删除预定
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void deleteBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int result = bookService.deleteBook(id);
        if (result > 0) {
            resp.getWriter().write("true");
        } else {
            resp.getWriter().write("false");
        }
    }

    /**
     * 预定列表
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void bookList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 这里可以添加处理 bookList 的逻辑
        String phone = req.getParameter("phone");
        if (phone == null) {
            phone = ""; // 如果没有提供手机号，则默认为空
        }
        List<Guest> guestList = guestService.getGuestsByPhone(phone);

        // 转换为JSON并返回
        String json = new Gson().toJson(guestList);
        resp.getWriter().write(json);
    }

    /**
     * 添加预定
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            Book book = FormBeanUtils.populate(req, Book.class);
            int result = bookService.addBook(book);
            if (result > 0) {
                resp.getWriter().write("true");
            } else {
                resp.getWriter().write("false");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 加载数据，下拉框绑定
     *
     * @param resp
     * @throws IOException
     */
    private void loadData(HttpServletResponse resp) throws IOException {
        List<Vip> allVips = vipsService.getAllVips();
        List<Home> homeByState = homeService.getHomeByState("空房");
        Map<String, Object> data = new HashMap<>();
        data.put("vips", allVips);
        data.put("homes", homeByState);
        String json = new Gson().toJson(data);
        resp.getWriter().write(json);
    }
}
